<?php

namespace app\common\dao\system;

use app\common\model\system\SystemMenu as model;
use app\common\dao\BaseDao;

class SystemMenuDao extends BaseDao
{
    /**
    * 设置模型
    * @return model
    */
    protected function getModel(): string
    {
        return model::class;
    }


    public function search(array $where, string $type)
    {
        $query = ($this->getModel())::getDB()->where('type', $type)->order('sort DESC,menu_id ASC');
        if (isset($where['pid'])) $query->where('pid', (int)$where['pid']);
        if (isset($where['keyword'])) $query->whereLike('menu_name|route', "%{$where['keyword']}%");
        if (isset($where['is_menu'])) $query->where('is_menu', (int)$where['is_menu']);
        return $query;
    }


    public function getAllMenu(string $type)
    {
        return ($this->getModel())::getDB()->where('type', $type)->where('is_menu', 1)->order('sort DESC,menu_id ASC')->select()->toArray();
    }


    public function getAll(string $type)
    {
        return ($this->getModel())::getInstance()->where('type', $type)->order('sort DESC,menu_id ASC')->select()->toArray();
    }


    public function menuExists(int $id, string $type)
    {
        return ($this->getModel())::getDB()->where($this->getPk(), $id)->where('is_menu', 1)->where('type', $type)->count() > 0;
    }


    public function mExists(int $id, string $type)
    {
        return ($this->getModel())::getDB()->where($this->getPk(), $id)->where('type', $type)->count() > 0;
    }


    public function authExists(int $id, string $type)
    {
        return ($this->getModel())::getDB()->where($this->getPk(), $id)->where('is_menu', 0)->where('type', $type)->count() > 0;
    }

    public function routeExists(string $route, string $type)
    {
        return ($this->getModel())::getDB()->where('route', $route)->where('is_menu', 0)->where('type', $type)->count() > 0;
    }


    public function getAllMenuOptions(string $type)
    {
        return ($this->getModel())::getDB()->where('is_menu', 1)->where('type', $type)->order('sort DESC,menu_id ASC')->column('menu_name,pid', 'menu_id');
    }


    public function ruleByMenuList(array $rule, string $type)
    {
        $paths = ($this->getModel())::getDB()->whereIn($this->getPk(), $rule)->column('path', 'menu_id');
        $ids = [];
        foreach ($paths as $id => $path) {
            $ids = array_merge($ids, explode('/', trim($path, '/')));
            array_push($ids, $id);
        }
        return ($this->getModel())::getDB()->where('is_menu', 1)->where('is_show', 1)->order('sort DESC,menu_id ASC')->where('type', $type)
            ->whereIn('menu_id', array_unique(array_filter($ids)))
            ->column('menu_name,route,params,icon,pid,menu_id');
    }


    public function getValidMenuList($type)
    {
        return ($this->getModel())::getDB()->where('is_menu', 1)->where('is_show', 1)->order('sort DESC,menu_id ASC')->where('type', $type)
            ->column('menu_name,route,params,icon,pid,menu_id');
    }


    public function getAllOptions($type)
    {
        return ($this->getModel())::getDB()->where('type', $type)->order('sort DESC,menu_id ASC')->column('menu_name,pid', 'menu_id');
    }


    public function getPath($id)
    {
        return ($this->getModel())::getDB()->where('menu_id', $id)->value('path');
    }


    public function getMenu(int $id,string $type)
    {
        return ($this->getModel())::getDB()->where('type', $type)->where('is_menu', 1)->where($this->getPk(), $id)->find();
    }


    public function getAuth(int $id, string $type)
    {
        return ($this->getModel())::getDB()->where('type', $type)->where('is_menu', 0)->where($this->getPk(), $id)->find();
    }


    public function delete(int $id): int
    {
        return ($this->getModel())::getDB()->delete($id);
    }


    public function pidExists(int $id)
    {
        return $this->fieldExists('pid', $id);
    }


    public function idsByRoutes(array $ids)
    {
        return ($this->getModel())::getDB()->where('is_menu', 0)->whereIn($this->getPk(), $ids)->column('params,route');
    }


    public function updatePath(string $oldPath, string $path)
    {
        ($this->getModel())::getDB()->whereLike('path', $oldPath . '%')->field('menu_id,path')->select()->each(function ($val) use ($oldPath, $path) {
            $newPath = str_replace($oldPath, $path, $val['path']);
            ($this->getModel())::getDB()->where('menu_id', $val['menu_id'])->update(['path' => $newPath]);
        });
    }


    public function getFieldExists($field,$value)
    {
        return (($this->getModel()::getDB())->where($field,$value)->find());
    }


    public function insertAll(array $data)
    {
        return ($this->getModel()::getDB())->insertAll($data);
    }

    public function deleteCommandMenu($where)
    {
        $this->getModel()::getDB()->where($where)->delete();
    }

    public function all()
    {
        return ($this->getModel()::getDB())->select();
    }

    public function getMenuPid(array $data)
    {
        return ($this->getModel()::getDB())->where('route','in',$data)->field('menu_id,pid,path')->order('path ASC')->select()->toArray();
    }


}